package com.oanda.blackberry.view;

import com.oanda.blackberry.restapi.Account;
import com.oanda.blackberry.restapi.AccountStatus;
import com.oanda.blackberry.ui.BitmapButtonField;
import com.oanda.blackberry.ui.ColoredEditField;
import com.oanda.blackberry.ui.OandaMainScreen;
import com.oanda.blackberry.util.AbstractViewPanel;
import com.oanda.blackberry.util.DefaultController;
import com.oanda.blackberry.util.Model;
import com.oanda.blackberry.util.PropertyChangeEvent;
import com.oanda.blackberry.util.StringUtils;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class AccountDetails extends OandaMainScreen implements
		AbstractViewPanel {
	DefaultController controller;
	String title = "Account Details";

	private boolean active = false;

	BitmapButtonField useThisAccount = new BitmapButtonField(
			Bitmap.getBitmapResource("activate-button-hover.png"),
			Bitmap.getBitmapResource("activate-button.png"), "Submit",
			12884967424L);

	MenuItem activate = new MenuItem("Activate", 9, 1001) {
		public void run() {
			AccountDetails.this.useThisAccount();
		}
	};

	MenuItem back = new MenuItem("Back", 10, 1001) {
		public void run() {
			AccountDetails.this.onClose();
		}
	};

	public AccountDetails(DefaultController controller) {
		this.controller = controller;

		updateTitle(this.title);
		this.useThisAccount.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				AccountDetails.this.useThisAccount();
			}
		});
		this.useThisAccount.setMargin(20, 0, 40, 0);
		this.useThisAccount.setPadding(20, 0, 20, 0);
	}

	private void useThisAccount() {
		AccountDetails account = this;
		this.controller.useAccount(((EditField) getField(1)).getText());
		this.controller.popScreen(this);
	}

	private void setBg() {
		Bitmap bg = Bitmap
				.getBitmapResource("account-details-480x600-pattern-c128.png");

		getMainManager().setBackground(
				BackgroundFactory.createBitmapBackground(bg));
	}

	private void setAccountStatus(final AccountStatus account) {
     AccountDetails screen = this;
     Runnable runnable = new Runnable() {
       private final AccountStatus _account = account;
 
       public void run() { AccountDetails.this.setBg();
 
         AccountDetails.this.deleteAll();
 
         Font font = DefaultController.getComponentFont().derive(0, 26);
 
         String nameS = Model.getModel().getAccountNameById(this._account.getId());
 
         String homeCurrency = Model.getModel().getAccountCurrencyById(this._account.getId());
         AccountDetails.this.updateTitle(AccountDetails.this.title + "(" + (homeCurrency == null ? "" : homeCurrency) + ")");
 
         if ((nameS == null) || ((nameS != null) && (nameS.length() == 0))) nameS = "---";
 
         ColoredEditField name = new ColoredEditField(9549568, "", nameS);
         ColoredEditField id = new ColoredEditField(9549568, "", this._account.getId());
 
         String bal = StringUtils.formatCurrency(StringUtils.columnRound(this._account.getBalance(), 2)) + (homeCurrency == null ? "" : new StringBuffer().append(" ").append(homeCurrency).toString());
 
         ColoredEditField balance = null;
         int balLimit = 165;
         if (font.getAdvance(bal) > balLimit) {
           balance = new ColoredEditField(9549568, "", bal, 5L);
         }
         else
         {
           balance = new ColoredEditField(9549568, "", bal);
         }
 
         ColoredEditField oO = new ColoredEditField(9013641, "", StringUtils.formatCurrency(this._account.getOpen_orders()));
         ColoredEditField oT = new ColoredEditField(9013641, "", StringUtils.formatCurrency(this._account.getOpen_trades()));
 
         ColoredEditField up = new ColoredEditField(9013641, "", StringUtils.formatCurrency(StringUtils.columnRound(this._account.getUnrealized_pl(), 2)));
 
         ColoredEditField rp = new ColoredEditField(9013641, "", StringUtils.formatCurrency(StringUtils.columnRound(this._account.getRealized_pl(), 2)));
         ColoredEditField mu = new ColoredEditField(9013641, "", StringUtils.formatCurrency(StringUtils.columnRound(this._account.getMargin_used(), 2)));
         ColoredEditField ma = new ColoredEditField(9013641, "", StringUtils.formatCurrency(StringUtils.columnRound(this._account.getMargin_avail(), 2)));
         ColoredEditField nav = new ColoredEditField(9013641, "", StringUtils.formatCurrency(StringUtils.columnRound(this._account.getNav(), 2)));
 
         name.setFont(font);
         id.setFont(font);
         balance.setFont(font);
         oO.setFont(font);
         oT.setFont(font);
         up.setFont(font);
         rp.setFont(font);
         mu.setFont(font);
         ma.setFont(font);
         ma.setFont(font);
         nav.setFont(font);
 
         name.setEditable(false);
         id.setEditable(false);
         balance.setEditable(false);
         oO.setEditable(false);
         oT.setEditable(false);
         up.setEditable(false);
         rp.setEditable(false);
         mu.setEditable(false);
         ma.setEditable(false);
         nav.setEditable(false);
 
         XYEdges padding = new XYEdges(10, 0, 11, 275);
         XYEdges padding2 = new XYEdges(10, 0, 11, 220);
         name.setPadding(50, 0, 11, 275);
         id.setPadding(padding);
         if (font.getAdvance(bal) > balLimit)
           balance.setPadding(padding2);
         else {
           balance.setPadding(padding);
         }
 
         up.setPadding(56, 0, 11, 275);
         rp.setPadding(padding);
         mu.setPadding(padding);
         ma.setPadding(padding);
         nav.setPadding(padding);
 
         AccountDetails.this.add(name);
         AccountDetails.this.add(id);
         AccountDetails.this.add(balance);
 
         AccountDetails.this.add(up);
         AccountDetails.this.add(rp);
         AccountDetails.this.add(mu);
         AccountDetails.this.add(ma);
         AccountDetails.this.add(nav);
 
         if (Model.getModel().getAccount().getAccountEntrys().length > 1) {
           if (!this._account.getId().equals(Model.getModel().getActiveAccountId())) {
             //AccountDetails.access$202(AccountDetails.this, false);
             AccountDetails.this.active = false;
             AccountDetails.this.add(AccountDetails.this.useThisAccount);
           } else {
//             AccountDetails.access$202(AccountDetails.this, true);
        	   AccountDetails.this.active = true;
           }
         }
         else AccountDetails.this.active = true;;
       }
     };
     UiApplication.getUiApplication().invokeLater(runnable);
   }

	public void modelPropertyChange(PropertyChangeEvent evt) {
		if ((evt.getPropertyName() != null)
				&& (evt.getPropertyName().equals("showAccountDetailsView"))) {
			if ((evt.getNewValue() != null)
					&& ((evt.getNewValue() instanceof AccountStatus))) {
				AccountStatus a = (AccountStatus) evt.getNewValue();
				setAccountStatus(a);
			}
			this.controller.pushScreen(this);
		} else if ((evt.getPropertyName() != null)
				&& (evt.getPropertyName().equals("setAccountStatus"))
				&& (Model.getModel().getAccount().getAccountEntrys().length > 1)) {
			setAccountStatus((AccountStatus) evt.getNewValue());
		}
	}

	public boolean onClose() {
		this.controller.popScreen(this);
		return super.onClose();
	}

	protected void makeMenu(Menu menu, int instance) {
		if (!this.active)
			menu.add(this.activate);
		menu.add(this.back);
	}
}
